package com.ruoyi.web.controller.exam;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.vo.PageResult;
import com.ruoyi.exam.domain.ExamUnreal;
import com.ruoyi.exam.serive.IExamUnrealService;
import com.ruoyi.exam.vo.ExamUnrealAnswer;
import com.ruoyi.exam.vo.ExamUnrealListVO;
import com.ruoyi.questionBank.domain.Question;
import com.ruoyi.questionBank.service.IQuestionService;
import com.ruoyi.questionBank.vo.QuestionListVO;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.exam.vo.ExamUnrealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/exam/unreal")
public class ExamUnrealController extends BaseController {
    @Autowired
    private IExamUnrealService unrealService;
    @Autowired
    private ISysUserService userService;

    @GetMapping("/qlist")
    public AjaxResult getQuestions() {
        return AjaxResult.success(unrealService.getUnrealQuestionData(getUserId()));
    }

    @GetMapping("/info")
    public AjaxResult getInfo() {
        long uid = getUserId();
        ExamUnreal examUnreal = unrealService.getByUid(uid);
        if (examUnreal == null) {
            examUnreal = ExamUnreal.buildDefault(uid, -1);
            unrealService.add(examUnreal);
        }
        return AjaxResult.success(examUnreal);
    }

    @PostMapping("/submit")
    public AjaxResult submitAnswer(int qid, String answer) {
        long uid = getUserId();
        ExamUnrealAnswer as = unrealService.submitAnswer(uid, qid, answer);
        return AjaxResult.success(as);
    }

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('exam:unreal:list')")
    public TableDataInfo list(ExamUnrealListVO listVO) {
        PageResult<ExamUnreal> pageResult = unrealService.page(listVO);

        PageResult<ExamUnrealVO> tmp = new PageResult<>(pageResult.getPage(), pageResult.getPageSize(), pageResult.getTotal());

        if(pageResult.getTotal() > 0) {
            Long[] uids = pageResult.getData().stream().map(u -> u.getUid()).toArray(Long[]::new);
            List<SysUser> users = userService.selectUserByIds(uids);
            Map<Long, SysUser> map = users.stream().collect(Collectors.toMap(SysUser::getUserId, u -> u));

            List<ExamUnrealVO> list = pageResult.getData().stream().map(u -> {
                ExamUnrealVO vo = new ExamUnrealVO(u);
                SysUser user = map.get(vo.getUid());
                if (user != null) {
                    vo.setNickName(user.getNickName());
                }
                return vo;
            }).collect(Collectors.toList());

            tmp.addAll(list);
        }

        return getDataTable(tmp.convertToPage());
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('exam:unreal:update')")
    public AjaxResult update(ExamUnreal examUnreal) {
        unrealService.update(examUnreal);
        return AjaxResult.success();
    }

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('exam:unreal:add')")
    public AjaxResult add(ExamUnreal examUnreal) {
        unrealService.add(examUnreal);
        return AjaxResult.success();
    }
}
